package com.lanou.myapp.help;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.lanou.myapp.greendao.DaoMaster;
import com.lanou.myapp.greendao.DaoSession;

/**
 * Created by dllo on 16/1/22.
 */
public class DaoSingleton {

    private  DaoMaster daoMaster;
    private  DaoSession daoSession;
    private  SQLiteDatabase db;

    private Context context;
    private DaoMaster.DevOpenHelper devOpenHelper;


    //使用单例模式保证只有一个对象
    private DaoSingleton() {
        this.context = HelperApplication.getContext();
    }

    public static DaoSingleton getInstance() {
        return DaoSingletonHolder.instance;
    }

    public static final class DaoSingletonHolder {
        private static final DaoSingleton instance = new DaoSingleton();
    }

    public DaoMaster.DevOpenHelper getDevOpenHelper() {
        if (devOpenHelper == null) {
            devOpenHelper = new DaoMaster.DevOpenHelper(HelperApplication.getContext(), "comicHome.db", null);
        }
        return devOpenHelper;
    }

    private SQLiteDatabase getDb() {
        if (db == null) {
            db = getDevOpenHelper().getWritableDatabase();
        }
        return db;
    }

    public DaoMaster getDaoMaster() {
        if (daoMaster == null) {
            daoMaster = new DaoMaster(getDb());
        }
        return daoMaster;
    }

    public  DaoSession getDaoSession() {
        if (daoSession == null) {
            daoSession = getDaoMaster().newSession();
        }
        return daoSession;
    }


}
